package com.party.controller.sys.sysManage;

import com.party.common.Page;
import com.party.model.SysModel;
import com.party.model.SysNav;
import com.party.model.SysNode;
import com.party.service.sys.MenuService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 12-4-2
 * Time: 下午9:38
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping(value = "/sys/nodeManage")
public class NodeManageController {
    @Resource
    MenuService menusService;
    private int pageSize = 9;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView index(
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        ModelAndView modelAndView = new ModelAndView();
        List<SysModel> modelList = this.menusService.getModels(new SysModel());
        modelAndView.addObject("modelList",modelList);
        modelAndView.setViewName("sys/sysmange/sys_nodes");
        return modelAndView;
    }


    @RequestMapping(value = "/query")
    @ResponseBody
    public Map<String,Object> query(HttpServletRequest req) {
        Map<String, Object> map = new HashMap<String, Object>();
        SysNode sysNode = new SysNode();
        String pageIndexStr = req.getParameter("pageIndex");
        String name = req.getParameter("name");
        String modelidStr = req.getParameter("modelIdStr");
        String code = req.getParameter("code");
        int pageIndex = 1;
        if (pageIndexStr != null && !"".equals(pageIndexStr)) {
            pageIndex = Integer.parseInt(pageIndexStr);
        }
        if (modelidStr != null && !"".equals(modelidStr)) {
            sysNode.setModelid(Integer.parseInt(modelidStr));
        }

        sysNode.setName(name);
        sysNode.setCode(code);
        Page p = this.menusService.getNodeList(sysNode, pageSize, pageIndex);

        map.put("pageCount",p.getTotalPageCount() );
        map.put("tableData",p.getResult());
        return map;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public ModelAndView add(
            @RequestParam(value = "msg", defaultValue = "", required = false) String msg,
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        ModelAndView modelAndView = new ModelAndView();
        List<SysNav> sysNavs = menusService.getNavs();
        modelAndView.addObject("navs", sysNavs);

        modelAndView.setViewName("sys/sysmange/sys_nodes_add");
        return modelAndView;
    }


    @RequestMapping(value = "/addData", method = RequestMethod.POST)
    public ModelAndView addData(
            @ModelAttribute("sysNode") SysNode sysNode,
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        ModelAndView modelAndView = new ModelAndView();

        int num = 0;
        String msg = "";
        try {
            if (sysNode.getNodeid() != null) {
                num = menusService.updateNode(sysNode);
                if (num == 1) {
                    msg = "更新成功！";
                } else {
                    msg = "更新失败！";
                }
            } else {
                num = menusService.addNode(sysNode);
                if (num == 1) {
                    msg = "新增成功！";
                } else {
                    msg = "新增失败！";
                }
            }
        } catch (Exception e) {

        }

        modelAndView.addObject("msg", msg);
        modelAndView.setViewName("redirect:/sys/nodeManage/index.do");
        return modelAndView;
    }


    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> del(
            @RequestParam("ids") String ids,
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        int num = 0;
        try {
            num = menusService.delNode(ids);
        } catch (Exception e) {
            num = -1;
            e.printStackTrace();
        }

        Map<String, Object> modelMap = new HashMap<String, Object>();
        modelMap.put("num", num);
        return modelMap;
    }

    /**
     * 更新某节点 信息
     *
     * @param nodeId
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/update/{nodeId}", method = RequestMethod.GET)
    public ModelAndView update(
            @PathVariable(value = "nodeId") int nodeId,
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        ModelAndView modelAndView = new ModelAndView();

        SysNode tNode = menusService.getNode(nodeId);
        modelAndView.addObject("cruTNode", tNode);

        List<SysNav> tNavs = menusService.getNavs();
        modelAndView.addObject("navs", tNavs);

        SysModel selectedModel = menusService.getModel(tNode.getModelid());
        SysNav selectedNav = menusService.getNav(selectedModel.getNavid());

        List<SysModel> modelList = menusService.getModels(selectedModel);//同属于 navid 的所有模块
        modelAndView.addObject("models", modelList);

        modelAndView.addObject("selectedModel",selectedModel);
        modelAndView.addObject("selectedNav",selectedNav);

        modelAndView.setViewName("/sys/sysmange/sys_nodes_add");
        return modelAndView;
    }

}
